package com.yan.deezer.activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yan.deezer.R;
import com.yan.deezer.object.Artist;


public class ArtistListAdapter extends ArrayAdapter<Artist> implements OnItemClickListener
{
	private static final String TAG = "YAN_DEEZER_ARTIST_LIST_ADAPTER";

	protected LayoutInflater mInflater;
	protected List<Artist> mList;
	protected int mnViewResId;
	protected Artist currentArtist;
	protected DataManager mDataManager;
	protected Context mContext;
	protected articlesViewHolder holder ;
	protected ArtistAlbumListManager artistAlbumListManager;

	
	
	public ArtistListAdapter(Context context, List<Artist> list, int resId) 
	{
		super(context, resId, list);
		mInflater = LayoutInflater.from(context);
		mList = list;
		mnViewResId = resId;
		mDataManager = DataManager.getInstance();
		mContext = context;
	}

	@Override
	public int getCount() 
	{
		int size = 0;
		if(mList != null)
		{
			size = mList.size();
		}
		return size;
	}
	
	@Override
	public long getItemId(int position) 
	{
		return position;
	}


	static class articlesViewHolder
	{

		/*
		 * 
		 * 
		 * { "id": "27", "name": "Daft Punk", "link":
		 * "http://www.deezer.com/music/daft-punk", "picture":
		 * "http://api.deezer.com/2.0/artist/27/image", "nb_album": "48", "nb_fan":
		 * "271184", "radio": true, "type": "artist" }
		 */
		
		ImageView artistPicture;
		TextView artistName;

	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		holder=null;
		
		if(convertView == null)
		{
			convertView = mInflater.inflate(mnViewResId, null);
			holder = new articlesViewHolder();			
			holder.artistPicture = (ImageView) convertView.findViewById (R.id.artist_image);
			holder.artistName = (TextView) convertView.findViewById (R.id.artist_name);
	        
			convertView.setTag(holder);
		}
		else 
		{
			holder = (articlesViewHolder) convertView.getTag();
		}

		currentArtist = getItem(position);
				
		int id=Integer.parseInt(currentArtist.getId());
		
		holder.artistPicture.setTag(id);
		
		mDataManager.setBitmapToImageView(currentArtist.getPicture(), holder.artistPicture, id);
		
		holder.artistName.setText(currentArtist.getName());
		holder.artistName.setSelected(true);
		holder.artistName.setCompoundDrawablePadding(3); 
				
		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
	{
		Artist artist = getItem(position);
		if(artist != null){
			
			Intent intent = new Intent(mContext, ArtistAlbumListActivity.class);
			intent.putExtra("ARTIST_ID", artist.getId());
			mContext.startActivity(intent);
			
		}
			
			
	}


	
	
	

		
}

    
	
	

